import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class Test3 {

    public static void main(String[] args) throws InterruptedException {
        ReentrantLock locker = new ReentrantLock();
        Thread t1 = new Thread(()->{
            if(locker.tryLock()) {
                System.out.println("A线程获取到锁");
            }else {
                System.out.println("A线程没有获取到锁");
            }
        },"A");
        Thread t2 = new Thread(()->{
            try {
                if (locker.tryLock(300, TimeUnit.MILLISECONDS)) {
                    System.out.println("B线程没有获取到锁");
                } else {
                    System.out.println("B线程获取到锁");
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"B");
        t1.start();
        t2.start();

    }
    public static void main2(String[] args) {
        ReentrantLock locker = new ReentrantLock();//创建锁对象
        try {
            //1.加锁
            locker.lock();
            //2.内容

        }finally {
            //3.解锁
            locker.unlock();
        }
    }
    public static void main1(String[] args) {
        ReentrantLock locker = new ReentrantLock();
        //1.加锁
        locker.lock();
        //2.内容

        //3.解锁
        locker.unlock();
    }
}
